package com.jltx.xq.zeus.startup

import android.os.Process
import com.jltx.xq.zeus.startup.manager.ExecutorManager
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executor
import java.util.concurrent.Executors

/**
 * @author aidenquan
 *         Created by junlintianxia on 2022/07/15.
 */
abstract class AndroidStartup<T> : Startup<T> {
    /**
     * 根据依赖的父任务进行入度数初始化
     */
    private val waitCountDown by lazy {
        CountDownLatch(getDependenciesCount())
    }

    override fun toWait() {
        try {
            waitCountDown.await()
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }
    }

    override fun toNotify() {
        waitCountDown.countDown()
    }

    override fun createExecutor(): Executor {
        //默认指定IO密集型
        return ExecutorManager.instance.ioExecutor
    }

    override fun dependenciesByName(): List<String>? {
        return null
    }

    override fun getDependenciesCount(): Int {
        return dependenciesByName()?.size ?: 0
    }

    override fun onDependenciesCompleted(startup: Startup<*>, result: Any?) {

    }

    override fun getThreadPriority(): Int = Process.THREAD_PRIORITY_DEFAULT

    override fun callCreateOnMainThread(): Boolean = false

    override fun waitOnMainThread(): Boolean  = false
}